<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="../common.inc" %>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%java.util.Random r=new java.util.Random();  %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link href="./css/style1.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="./js/form.js"></script>
<script type="text/javascript" language="javascript">
<!--
	function checkForm2(){
		var objs = document.getElementsByName("id");
		var sign = 0;
		for(var i = 0; i < objs.length; i++){
			if(objs[i].value != 0 && objs[i].checked)sign = sign + 1;
		}
		if(sign <= 0){
			alert("请选择待操作的项！");
			return false;
		}else if(sign > 1){
			alert("只能选择一个待操作项！");
			return false;
		}
		return true;
	}

	function deptSaleRecord(){
		window.location.href = "./saleRecord.do?action=deptSaleRecord";
		window.location.target = "main";
	}
	
	function addSaleRecord(){
		var objs = document.getElementsByName("id");
		var sign = 0;
		for(var i = 0; i < objs.length; i++){
			if(objs[i].value != 0 && objs[i].checked)sign = sign + 1;
		}
		if(sign <= 0){
			window.location.href = "./saleRecord.do?action=preAddSaleRecord&sign=2";
			window.location.target = "main";
		}else{
			addSaleRecordOne();
		}
	}
	
	function addSaleRecordOne(){
		if(checkForm2()){
			var objs = document.getElementsByName("id");
			var id = 0;
			for(var i = 0; i < objs.length; i++){
				if(objs[i].value != 0 && objs[i].checked){
					id = objs[i].value;
					break;
				}
			}
			var isWrite = document.getElementById("share_"+id);
			if(isWrite.value != "" && isWrite.value == '1'){
				window.location.href = "./saleRecord.do?action=preAddSaleRecordOne&customerId="+id+"&sign=2";
				window.location.target = "main";
			}else{
				alert("您对此共享客户只有可读权限，不能新增对应的销售日志！");
			}
		}
	}
	
	function updateSaleRecord(){
		if(checkForm2()){
			var objs = document.getElementsByName("id");
			var id = 0;
			for(var i = 0; i < objs.length; i++){
				if(objs[i].value != 0 && objs[i].checked){
					id = objs[i].value;
					break;
				}
			}
			var isWrite = document.getElementById("share_"+id);
			var isHangup = document.getElementById("hangup_"+id);
			if(isWrite.value != "" && isWrite.value == '1'){
				if(isHangup.value != "1" && isHangup.value != "2" && isHangup.value != "21"){
					window.location.href = "./saleRecord.do?action=preUpdateSaleRecord&customerId="+id+"&sign=2";
					window.location.target = "main";
				}else{
					alert("此客户已被挂起，不能修改！");
				}
			}else{
				alert("您对此共享客户只有可读权限，不能修改对应的销售日志！");
			}
		}
	}

	function addSaleRecordHangup(){
		if(checkForm2()){
			var objs = document.getElementsByName("id");
			var id = 0;
			for(var i = 0; i < objs.length; i++){
				if(objs[i].value != 0 && objs[i].checked){
					id = objs[i].value;
					break;
				}
			}
			var isWrite = document.getElementById("share_"+id);
			var isHangup = document.getElementById("hangup_"+id);
			var isSaleStage = document.getElementById("saleStage_"+id);
			if(isWrite.value != "" && isWrite.value == '1'){
				if(isHangup.value != "1" && isHangup.value != "2" && isHangup.value != "21"){
					if(isSaleStage.value != "" && isSaleStage.value > 0){
						document.saleRecordForm.action = "./saleRecord.do?action=preAddSaleRecordHangup&customerId="+id+"&sign=2";
						document.saleRecordForm.target = "main";
						document.saleRecordForm.submit();
					}else{
						alert("此客户没有商机跟进记录，不能被挂起！");
					}
				}else{
					alert("此客户已被挂起！");
				}
			}else{
				alert("您对此共享客户只有可读权限，不能挂起客户！");
			}
		}
	}

	function addSaleRecordClose(){
		if(checkForm2()){
			var objs = document.getElementsByName("id");
			var id = 0;
			for(var i = 0; i < objs.length; i++){
				if(objs[i].value != 0 && objs[i].checked){
					id = objs[i].value;
					break;
				}
			}
			var isWrite = document.getElementById("share_"+id);
			var isSaleStage = document.getElementById("saleStage_"+id);
			if(isWrite.value != "" && isWrite.value == '1'){
				if(isSaleStage.value != "" && isSaleStage.value > 0){
					document.saleRecordForm.action = "./saleRecord.do?action=preAddSaleRecordClose&customerId="+id+"&sign=2";
					document.saleRecordForm.target = "main";
					document.saleRecordForm.submit();
				}else{
					alert("此客户没有商机跟进记录，不需要关闭商机！");
				}
			}else{
				alert("您对此共享客户只有可读权限，不能关闭商机！");
			}
		}
	}
		
	function advancedQueryDialog(){
		var url = "./saleRecord.do?action=advancedQuery&radom="+<%=r.nextInt(100)%>;
		var dlgParam = "dialogHeight:360px;dialogWidth:450px;dialogTop:300px;dialogLeft:400px;status:no;help:no;scrollbars:yes;";
		var rt = window.showModalDialog(url,"",dlgParam);
		if(rt != undefined){
			//根据从子窗口得到的值，从中提取customerName,industryId,areaId,customerSourceId,saleStageId,
			//startPredictCost,endPredictCost,startPredictContractDate,endPredictContractDate,startContactDate,endContactDate
			var tStr = rt.split(",");
			document.getElementById("customerName2").value = tStr[0];
			document.getElementById("industryId").value = tStr[1];
			document.getElementById("areaId").value = tStr[2];
			document.getElementById("customerSourceId").value = tStr[3];
			document.getElementById("saleStageId").value = tStr[4];
			document.getElementById("startPredictCost").value = tStr[5];
			document.getElementById("endPredictCost").value = tStr[6];
			document.getElementById("startPredictContractDate").value = tStr[7];
			document.getElementById("endPredictContractDate").value = tStr[8];
			document.getElementById("startContactDate").value = tStr[9];
			document.getElementById("endContactDate").value = tStr[10];
			
			document.getElementById("sFlag").value = "2";
			document.saleRecordForm.action = "./saleRecord.do";
			document.saleRecordForm.target = "main";
			document.saleRecordForm.submit();
		}
	}
	
	function viewSaleRecord(){
		if(checkForm2()){
			var objs = document.getElementsByName("id");
			var id = 0;
			for(var i = 0; i < objs.length; i++){
				if(objs[i].value != 0 && objs[i].checked){
					id = objs[i].value;
					break;
				}
			}
			window.location.href = "./saleRecord.do?action=viewSaleRecord&customerId="+id;
			window.location.target = "main";
		}
	}
	
	function searchCustomer(){
		var customerName = document.getElementById("customerName");
		if(customerName.value == ""){
			alert("客户关键字不能为空！");
		}else{
			document.getElementById("sFlag").value = "1";
			document.saleRecordForm.action = "./saleRecord.do";
			document.saleRecordForm.target = "main";
			document.saleRecordForm.submit();
		}
	}
	
	function pageCustomer(flag){
		document.saleRecordForm.action = "./saleRecord.do?action=display&flag="+flag;
		document.saleRecordForm.target = "main";
		document.saleRecordForm.submit();
	}
	
	function initPage(){
		if("<c:out value='${isNotUpdate}'/>" == '1'){
			alert("对不起，联系记录已经被锁定！");
		}

		if("<c:out value='${deleteResult}'/>" == 2){
			alert("当前客户已挂起！");
		}else if("<c:out value='${deleteResult}'/>" == 3){
			alert("当前客户已关闭商机！");
		}
	}
-->
</script>
</head>
<body onload="initPage()">
<form name="saleRecordForm" action="" method="post">
	<table class="table_frame">
		<tr>
			<td>
				<table class="table_title1">
					<tr>
						<td class="title_lab">销售日志</td>
					</tr>
					<tr>
						<td valign="bottom" class="a_tools" style="padding-left: 3px;"><fmt:message key='openSea.lable.search'/>
							<input id="customerName" name="customerName" class="input_query" type="text" value="${customerName}"/>
							<a class="search" href="javascript:searchCustomer();" target="main" hidefocus="true">查询</a>
							|
							<a class="asearch" href="javascript:advancedQueryDialog();" target="main" hidefocus="true">高级查询</a>
							|
							<a class="vsalerecord" href="javascript:viewSaleRecord();" hidefocus="true">查看日志</a>
							|
							<a class="add" href="javascript:addSaleRecord();" hidefocus="true">新增</a>
							<a class="update" href="javascript:updateSaleRecord();" hidefocus="true">修改</a>
							<a class="hangup" href="javascript:addSaleRecordHangup();" hidefocus="true">挂起</a>
							<a class="closestage" href="javascript:addSaleRecordClose();" hidefocus="true">关闭商机</a>
						</td>
						<!-- 
						<td valign="bottom" align="right">
							<c:if test="${isViewDeptSalesLog == 1}">
								<a href="javascript:deptSaleRecord();" hidefocus="true">查看部门销售日志</a>&nbsp;&nbsp;
							</c:if>
						</td>
						 -->
					</tr>
					<tr><td height="2"></td></tr>
				</table>
			</td>
		</tr>
		<tr>
			<td>
				<table class="table_title2">
					<tr>
						<td class="title_lab">销售日志</td>
						<td align="right" valign="">
							<!-- page -->
							<table class="table_page2">
								<tr>
									<td valign="bottom">
										<img src="./images/frist.bmp" onclick="pageCustomer(1)" alt="首页" style="vertical-align: middle;cursor: hand;">
										<img src="./images/last.bmp" onclick="pageCustomer(2)" alt="上一页" style="vertical-align: middle;cursor: hand;">&nbsp;
										<font color="red">${currentPage}</font>/${totalPages}&nbsp;<fmt:message key='common.page'/>&nbsp;
										<img src="./images/next.bmp" onclick="pageCustomer(3)" alt="下一页" style="vertical-align: middle;cursor: hand;">
										<img src="./images/end.bmp" onclick="pageCustomer(4)" alt="末页" style="vertical-align: middle;cursor: hand;">
										共&nbsp;&nbsp;${totalRows}&nbsp;<fmt:message key='common.page.totalRow'/>&nbsp;&nbsp;&nbsp;
									</td>
								</tr>
							</table>
						</td>
					</tr>
					<tr><td colspan="3" height="2"></td></tr>
					<tr>
						<td colspan="2">
							<table class="table">
								<tr>
									<td valign="top">
										<!-- 
										<div style="width:800px;height:100%;overflow-x: scroll;overflow-y: hidden;">
										 -->
										<table class="table_style">
											<tr>
												<td class="title" width="8"></td>
												<td class="title" width="30">
													选择<!--<input id="id" name="id" type="checkbox" value="0" onclick="checkAll('id')"/> -->
												</td>
												<td class="title" width="30">共享</td>
												<!-- 
												<td class="title4" width="8%">状态</td>
												 -->
												<td class="title">客户名称</td>
												<td class="title" width="12%">销售阶段</td>
												<td class="title" width="12%">销售代表</td>
												<td class="title" width="15%">最后联系</td>
											</tr>
											<c:choose>
											<c:when test="${not empty customers}">
											<c:forEach var="customer" items="${customers}">
											<tr onmouseover="onmousemoveChange(this,'img${customer.id}')" onmouseout="onmouseoutChange(this,'img${customer.id}')">
												<td height="16" width="8" align="right"><img id="img${customer.id}" src="./images/table_sign2.bmp"></td>
												<td>
													<input id="id" name="id" type="checkbox" value="${customer.id}"/>
													<input id="share_${customer.id}" name="share_${customer.id}" type="hidden" value="${customer.isWrite}"/>
													<input id="hangup_${customer.id}" name="hangup_${customer.id}" type="hidden" value="${customer.saleStatus}"/>
													<input id="saleStage_${customer.id}" name="saleStage_${customer.id}" type="hidden" value="${customer.saleStageId}"/>
												</td>
												<td align="center">
													<c:if test="${customer.isShare == 1}">
													<img class="btn_img" src="./images/share.gif" onclick="" title="<fmt:message key='customer.lable.share'/>">
													</c:if>
												</td>
												<!-- 
												<td align="center">
													<c:if test="${customer.isFollow == 1}">
													<img class="btn_img" src="./images/follow.gif" onclick="" title="<fmt:message key='customer.lable.follow'/>">&nbsp;&nbsp;
													</c:if>
												</td>
												 -->
												<td align="left"><a href="./customerManage.do?action=customerDetail&customerId=${customer.id}&sign=71" target="main" hidefocus="true">${customer.customerName}</td>
												<td>
													${customer.saleStage}
													<font color="red">
													<c:choose>
														<c:when test="${customer.saleStatus == '1'}">[挂起]</c:when>
														<c:when test="${customer.saleStatus == '4'}">[关闭商机]</c:when>
													</c:choose>
													</font>
												</td>
												<td>${customer.salePerson}</td>
												<td><fmt:formatDate value="${customer.lastLink}" pattern="yyyy-MM-dd"/></td>
											</tr>
											</c:forEach>
											</c:when>
											<c:otherwise><tr><td colspan="6" align="center">暂无信息！</td></tr></c:otherwise>
											</c:choose>
										</table>
										<!-- 
										</div>
										 -->
									</td>
								</tr>
								<tr>
									<td align="left" valign="bottom">
										<!-- page -->
										<table id="tp" class="table_page">
											<tr>
												<td>&nbsp;&nbsp;
													<img src="./images/frist.bmp" onclick="pageCustomer(1)" alt="首页" style="vertical-align: middle;cursor: hand;">
													<img src="./images/last.bmp" onclick="pageCustomer(2)" alt="上一页" style="vertical-align: middle;cursor: hand;">&nbsp;
													<font color="red">${currentPage}</font>/${totalPages}&nbsp;<fmt:message key='common.page'/>&nbsp;
													<img src="./images/next.bmp" onclick="pageCustomer(3)" alt="下一页" style="vertical-align: middle;cursor: hand;">
													<img src="./images/end.bmp" onclick="pageCustomer(4)" alt="末页" style="vertical-align: middle;cursor: hand;">
													共&nbsp;&nbsp;${totalRows}&nbsp;<fmt:message key='common.page.totalRow'/>
												</td>
											</tr>
										</table>
									</td>
								</tr>
							</table>
						</td>
					</tr>
				</table>
			</td>
		</tr>
	</table>
	<input id="customerName2" name="customerName2" type="hidden" value="${queryCustomer.customerName2}"/>
	<input id="industryId" name="industryId" type="hidden" value="${queryCustomer.industryId}"/>
	<input id="areaId" name="areaId" type="hidden" value="${queryCustomer.areaId}"/>
	<input id="customerSourceId" name="customerSourceId" type="hidden" value="${queryCustomer.customerSourceId}"/>
	<input id="saleStageId" name="saleStageId" type="hidden" value="${queryCustomer.saleStageId}"/>
	<input id="startPredictCost" name="startPredictCost" type="hidden" value="${queryCustomer.startPredictCost}"/>
	<input id="endPredictCost" name="endPredictCost" type="hidden" value="${queryCustomer.endPredictCost}"/>
	<input id="startPredictContractDate" name="startPredictContractDate" type="hidden" value="${queryCustomer.startPredictContractDate}"/>
	<input id="endPredictContractDate" name="endPredictContractDate" type="hidden" value="${queryCustomer.endPredictContractDate}"/>
	<input id="startContactDate" name="startContactDate" type="hidden" value="${queryCustomer.startContactDate}"/>
	<input id="endContactDate" name="endContactDate" type="hidden" value="${queryCustomer.endContactDate}"/>
	<input id="sFlag" name="sFlag" type="hidden" value="${sFlag}"/>
</form>
</body>
</html>